package com.debao.quartz.task;

import com.debao.common.utils.DateUtils;
import com.debao.system.domain.StadiumTable;
import com.debao.system.domain.StadiumTableSchedule;
import com.debao.system.service.IStadiumTableService;
import com.debao.system.service.IStadiumTableScheduleService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;


/**
 * 场馆的定时任务
 */
@Component("stadium")
public class StadiumTask {

    private static final Integer DAY = 7;

    @Resource
    private IStadiumTableService stadiumTableService;
    @Resource
    private IStadiumTableScheduleService tableScheduleService;

    /**
     * 桌台添加排期
     */
    public void addSchedule() {
        // 获取桌台
        List<StadiumTable> list = stadiumTableService.selectStadiumTableList(new StadiumTable());

        for (StadiumTable stadiumTable : list) {
            // 生成排期
            createSchedule(stadiumTable);
        }
    }

    /**
     * 生成后几天的排期
     *
     * @param stadiumTable
     */
    private void createSchedule(StadiumTable stadiumTable) {
        // 获取后Day天的日期
        List<LocalDate> dayList = DateUtils.getAfterDayDate(DAY);
        dayList.forEach(localDate -> {
            postData(stadiumTable, localDate);
        });
    }

    private void postData(StadiumTable stadiumTable, LocalDate localDate) {
        int hour = DateUtils.getDiffHour(stadiumTable.getStartAt(), stadiumTable.getEndAt());
        for (int i = 0; i < hour; i++) {
            tableScheduleService.insertStadiumTableSchedule(new StadiumTableSchedule()
                    .setStadiumId(stadiumTable.getStadiumId())
                    .setStadiumTableId(stadiumTable.getId())
                    .setDate(localDate.toString())
                    .setTime(DateUtils.addMoreHour(stadiumTable.getStartAt(), i)));
        }
    }

}
